#include <QPainter>
#include <QPainterPath>
#include <QPainterPathStroker>

#include "Edge.h"
#include "Wave.h"

Edge::Edge(double x1, double y1,
           double x2, double y2,
           QColor _lines_color) {
    sourcePoint = new QPointF(x1, y1);
    destPoint = new QPointF(x2, y2);
    lines_color = _lines_color;
}

QRectF Edge::boundingRect() const
{
    if (!sourcePoint || !destPoint)
        return QRectF();

    return QRectF((*sourcePoint), QSizeF(destPoint->x() - sourcePoint->x(),
                                          destPoint->y() - sourcePoint->y()));
}

QPainterPath Edge::shape() const {
    QPainterPath path(*sourcePoint);
    QPainterPathStroker stroker;
    path.lineTo(*destPoint);
    stroker.setWidth(5);
    return stroker.createStroke(path);
}

void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (!sourcePoint || !destPoint)
        return;

    QLineF line(*sourcePoint, *destPoint);
    if (qFuzzyCompare(line.length(), qreal(0.)))
        return;

    // Draw the line itself.
    painter->setPen(QPen(lines_color, 1, Qt::SolidLine,
                                       Qt::RoundCap, Qt::RoundJoin));


    // TODO(Lega):: add here drawing a curve using QPainterPath.quadTo if edge is composite.
    painter->drawLine(line);
}
